﻿var modalDeleteProject;

$().ready(function () {
    modalDeleteProject = new ModalDeleteProject();
    modalDeleteProject.init();
});

function ModalDeleteProject() {
    var _this = this;
    var ajaxUrl = "/Project/Delete";
    this.init = function () {

        $(".delete").click(function () {
            var item = $(this).siblings(".project-id");
            _this.isDeleted(item.val());
        });
    }

    this.isDeleted = function (ID) {
        var dataAjax = {
            projectID: ID
        }

        $.ajax(
        {
            type: "POST",
            url: "/Project/IsDeleted",
            data: dataAjax,
            success: function (data) {
                if (data.result == false) {
                    OpenConfirm("Are you sure you want to delete the project?", function () {
                        _this.execute(ID);
                    });
                }
                else {
                    SuccessMessageBox("The project can not be removed");
                }
            }
        });
    }

    this.execute = function (ID) {
        var dataAjax = {
            projectID: ID
        }

        $.ajax(
        {
            type: "POST",
            url: ajaxUrl,
            data: dataAjax,
            success: function (data) {
                if (data.result == true) {
                    window.location.replace("/Project/List");
                }
                else {
                }
            },
            error: function () {
                alert("Ошибка");
            }
        });
    }
}